<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"> <style>
	.KEYW {color: #933;}
	.COMM {color: #bbb; font-style: italic;}
	.NUMB {color: #393;}
	.STRN {color: #393;}
	.REGX {color: #339;}
	.line {border-right: 1px dotted #666; color: #666; font-style: normal;}
	</style></head><body><pre><span class='line'>  1</span> <span class="COMM">/*
<span class='line'>  2</span>  * Copyright (C) 2008, 2009 Mihai Şucan
<span class='line'>  3</span>  *
<span class='line'>  4</span>  * This file is part of PaintWeb.
<span class='line'>  5</span>  *
<span class='line'>  6</span>  * PaintWeb is free software: you can redistribute it and/or modify
<span class='line'>  7</span>  * it under the terms of the GNU General Public License as published by
<span class='line'>  8</span>  * the Free Software Foundation, either version 3 of the License, or
<span class='line'>  9</span>  * (at your option) any later version.
<span class='line'> 10</span>  *
<span class='line'> 11</span>  * PaintWeb is distributed in the hope that it will be useful,
<span class='line'> 12</span>  * but WITHOUT ANY WARRANTY; without even the implied warranty of
<span class='line'> 13</span>  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
<span class='line'> 14</span>  * GNU General Public License for more details.
<span class='line'> 15</span>  *
<span class='line'> 16</span>  * You should have received a copy of the GNU General Public License
<span class='line'> 17</span>  * along with PaintWeb.  If not, see &lt;http://www.gnu.org/licenses/>.
<span class='line'> 18</span>  *
<span class='line'> 19</span>  * $URL: http://code.google.com/p/paintweb $
<span class='line'> 20</span>  * $Date: 2009-07-01 18:44:56 +0300 $
<span class='line'> 21</span>  */</span><span class="WHIT">
<span class='line'> 22</span> 
<span class='line'> 23</span> </span><span class="COMM">/**
<span class='line'> 24</span>  * @author &lt;a lang="ro" href="http://www.robodesign.ro/mihai">Mihai Şucan&lt;/a>
<span class='line'> 25</span>  * @fileOverview Holds the ellipse tool implementation.
<span class='line'> 26</span>  */</span><span class="WHIT">
<span class='line'> 27</span> 
<span class='line'> 28</span> </span><span class="COMM">/**
<span class='line'> 29</span>  * @class The ellipse tool.
<span class='line'> 30</span>  *
<span class='line'> 31</span>  * @param {PaintWeb} app Reference to the main paint application object.
<span class='line'> 32</span>  */</span><span class="WHIT">
<span class='line'> 33</span> </span><span class="NAME">pwlib.tools.ellipse</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">app</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 34</span> </span><span class="WHIT">  </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">_self</span><span class="WHIT">         </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">this</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 35</span> </span><span class="WHIT">      </span><span class="NAME">clearInterval</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">app.win.clearInterval</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 36</span> </span><span class="WHIT">      </span><span class="NAME">config</span><span class="WHIT">        </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">app.config</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 37</span> </span><span class="WHIT">      </span><span class="NAME">context</span><span class="WHIT">       </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">app.buffer.context</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 38</span> </span><span class="WHIT">      </span><span class="NAME">gui</span><span class="WHIT">           </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">app.gui</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 39</span> </span><span class="WHIT">      </span><span class="NAME">image</span><span class="WHIT">         </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">app.image</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 40</span> </span><span class="WHIT">      </span><span class="NAME">MathMax</span><span class="WHIT">       </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">Math.max</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 41</span> </span><span class="WHIT">      </span><span class="NAME">MathMin</span><span class="WHIT">       </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">Math.min</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 42</span> </span><span class="WHIT">      </span><span class="NAME">mouse</span><span class="WHIT">         </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">app.mouse</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'> 43</span> </span><span class="WHIT">      </span><span class="NAME">setInterval</span><span class="WHIT">   </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">app.win.setInterval</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 44</span> 
<span class='line'> 45</span> </span><span class="WHIT">  </span><span class="COMM">/**
<span class='line'> 46</span>    * The interval ID used for invoking the drawing operation every few 
<span class='line'> 47</span>    * milliseconds.
<span class='line'> 48</span>    *
<span class='line'> 49</span>    * @private
<span class='line'> 50</span>    * @see PaintWeb.config.toolDrawDelay
<span class='line'> 51</span>    */</span><span class="WHIT">
<span class='line'> 52</span> </span><span class="WHIT">  </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">timer</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">null</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 53</span> 
<span class='line'> 54</span> </span><span class="WHIT">  </span><span class="COMM">/**
<span class='line'> 55</span>    * Tells if the &lt;kbd>Shift&lt;/kbd> key is down or not. This is used by the 
<span class='line'> 56</span>    * drawing function.
<span class='line'> 57</span>    *
<span class='line'> 58</span>    * @private
<span class='line'> 59</span>    * @type Boolean
<span class='line'> 60</span>    * @default false
<span class='line'> 61</span>    */</span><span class="WHIT">
<span class='line'> 62</span> </span><span class="WHIT">  </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">shiftKey</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">false</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 63</span> 
<span class='line'> 64</span> </span><span class="WHIT">  </span><span class="COMM">/**
<span class='line'> 65</span>    * Tells if the drawing canvas needs to be updated or not.
<span class='line'> 66</span>    *
<span class='line'> 67</span>    * @private
<span class='line'> 68</span>    * @type Boolean
<span class='line'> 69</span>    * @default false
<span class='line'> 70</span>    */</span><span class="WHIT">
<span class='line'> 71</span> </span><span class="WHIT">  </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">needsRedraw</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">false</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 72</span> 
<span class='line'> 73</span> </span><span class="WHIT">  </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">K</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">4</span><span class="PUNC">*</span><span class="PUNC">(</span><span class="PUNC">(</span><span class="NAME">Math.SQRT2</span><span class="PUNC">-</span><span class="NUMB">1</span><span class="PUNC">)</span><span class="PUNC">/</span><span class="NUMB">3</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 74</span> 
<span class='line'> 75</span> </span><span class="WHIT">  </span><span class="COMM">/**
<span class='line'> 76</span>    * Holds the starting point on the &lt;var>x&lt;/var> axis of the image, for the 
<span class='line'> 77</span>    * current drawing operation.
<span class='line'> 78</span>    *
<span class='line'> 79</span>    * @private
<span class='line'> 80</span>    * @type Number
<span class='line'> 81</span>    */</span><span class="WHIT">
<span class='line'> 82</span> </span><span class="WHIT">  </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">x0</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 83</span> 
<span class='line'> 84</span> </span><span class="WHIT">  </span><span class="COMM">/**
<span class='line'> 85</span>    * Holds the starting point on the &lt;var>y&lt;/var> axis of the image, for the 
<span class='line'> 86</span>    * current drawing operation.
<span class='line'> 87</span>    *
<span class='line'> 88</span>    * @private
<span class='line'> 89</span>    * @type Number
<span class='line'> 90</span>    */</span><span class="WHIT">
<span class='line'> 91</span> </span><span class="WHIT">  </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">y0</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 92</span> 
<span class='line'> 93</span> </span><span class="WHIT">  </span><span class="COMM">/**
<span class='line'> 94</span>    * Tool deactivation event handler.
<span class='line'> 95</span>    */</span><span class="WHIT">
<span class='line'> 96</span> </span><span class="WHIT">  </span><span class="NAME">this.deactivate</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 97</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">timer</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'> 98</span> </span><span class="WHIT">      </span><span class="NAME">clearInterval</span><span class="PUNC">(</span><span class="NAME">timer</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'> 99</span> </span><span class="WHIT">      </span><span class="NAME">timer</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">null</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>100</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>101</span> 
<span class='line'>102</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">mouse.buttonDown</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>103</span> </span><span class="WHIT">      </span><span class="NAME">context.clearRect</span><span class="PUNC">(</span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">image.width</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">image.height</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>104</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>105</span> 
<span class='line'>106</span> </span><span class="WHIT">    </span><span class="NAME">needsRedraw</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">false</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>107</span> 
<span class='line'>108</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">true</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>109</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>110</span> 
<span class='line'>111</span> </span><span class="WHIT">  </span><span class="COMM">/**
<span class='line'>112</span>    * Initialize the drawing operation.
<span class='line'>113</span>    *
<span class='line'>114</span>    * @param {Event} ev The DOM Event object.
<span class='line'>115</span>    */</span><span class="WHIT">
<span class='line'>116</span> </span><span class="WHIT">  </span><span class="NAME">this.mousedown</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">ev</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>117</span> </span><span class="WHIT">    </span><span class="COMM">// The mouse start position</span><span class="WHIT">
<span class='line'>118</span> </span><span class="WHIT">    </span><span class="NAME">x0</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">mouse.x</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>119</span> </span><span class="WHIT">    </span><span class="NAME">y0</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">mouse.y</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>120</span> 
<span class='line'>121</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">timer</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>122</span> </span><span class="WHIT">      </span><span class="NAME">timer</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">setInterval</span><span class="PUNC">(</span><span class="NAME">_self.draw</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">config.toolDrawDelay</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>123</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>124</span> </span><span class="WHIT">    </span><span class="NAME">shiftKey</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">ev.shiftKey</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>125</span> </span><span class="WHIT">    </span><span class="NAME">needsRedraw</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">false</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>126</span> 
<span class='line'>127</span> </span><span class="WHIT">    </span><span class="NAME">gui.statusShow</span><span class="PUNC">(</span><span class="STRN">'ellipseMousedown'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>128</span> 
<span class='line'>129</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">true</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>130</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>131</span> 
<span class='line'>132</span> </span><span class="WHIT">  </span><span class="COMM">/**
<span class='line'>133</span>    * Store the &lt;kbd>Shift&lt;/kbd> key state which is used by the drawing function.
<span class='line'>134</span>    *
<span class='line'>135</span>    * @param {Event} ev The DOM Event object.
<span class='line'>136</span>    */</span><span class="WHIT">
<span class='line'>137</span> </span><span class="WHIT">  </span><span class="NAME">this.mousemove</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">ev</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>138</span> </span><span class="WHIT">    </span><span class="NAME">shiftKey</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">ev.shiftKey</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>139</span> </span><span class="WHIT">    </span><span class="NAME">needsRedraw</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">true</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>140</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>141</span> 
<span class='line'>142</span> </span><span class="WHIT">  </span><span class="COMM">/**
<span class='line'>143</span>    * Perform the drawing operation. This function is called every few 
<span class='line'>144</span>    * milliseconds.
<span class='line'>145</span>    *
<span class='line'>146</span>    * &lt;p>Hold down the &lt;kbd>Shift&lt;/kbd> key to draw a circle.
<span class='line'>147</span>    * &lt;p>Press &lt;kbd>Escape&lt;/kbd> to cancel the drawing operation.
<span class='line'>148</span>    *
<span class='line'>149</span>    * @see PaintWeb.config.toolDrawDelay
<span class='line'>150</span>    */</span><span class="WHIT">
<span class='line'>151</span> </span><span class="WHIT">  </span><span class="NAME">this.draw</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>152</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">needsRedraw</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>153</span> </span><span class="WHIT">      </span><span class="KEYW">return</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>154</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>155</span> 
<span class='line'>156</span> </span><span class="WHIT">    </span><span class="NAME">context.clearRect</span><span class="PUNC">(</span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">image.width</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">image.height</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>157</span> 
<span class='line'>158</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">rectx0</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">MathMin</span><span class="PUNC">(</span><span class="NAME">mouse.x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">x0</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>159</span> </span><span class="WHIT">        </span><span class="NAME">rectx1</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">MathMax</span><span class="PUNC">(</span><span class="NAME">mouse.x</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">x0</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>160</span> </span><span class="WHIT">        </span><span class="NAME">recty0</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">MathMin</span><span class="PUNC">(</span><span class="NAME">mouse.y</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">y0</span><span class="PUNC">)</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>161</span> </span><span class="WHIT">        </span><span class="NAME">recty1</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">MathMax</span><span class="PUNC">(</span><span class="NAME">mouse.y</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">y0</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>162</span> 
<span class='line'>163</span> </span><span class="WHIT">    </span><span class="COMM">/*
<span class='line'>164</span>       ABCD - rectangle
<span class='line'>165</span>       A(rectx0, recty0), B(rectx1, recty0), C(rectx1, recty1), D(rectx0, recty1)
<span class='line'>166</span>     */</span><span class="WHIT">
<span class='line'>167</span> 
<span class='line'>168</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">w</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">rectx1</span><span class="PUNC">-</span><span class="NAME">rectx0</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>169</span> </span><span class="WHIT">        </span><span class="NAME">h</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">recty1</span><span class="PUNC">-</span><span class="NAME">recty0</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>170</span> 
<span class='line'>171</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">w</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="PUNC">!</span><span class="NAME">h</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>172</span> </span><span class="WHIT">      </span><span class="NAME">needsRedraw</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">false</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>173</span> </span><span class="WHIT">      </span><span class="KEYW">return</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>174</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>175</span> 
<span class='line'>176</span> </span><span class="WHIT">    </span><span class="COMM">// Constrain the ellipse to be a circle</span><span class="WHIT">
<span class='line'>177</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">shiftKey</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>178</span> </span><span class="WHIT">      </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">w</span><span class="WHIT"> </span><span class="PUNC">></span><span class="WHIT"> </span><span class="NAME">h</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>179</span> </span><span class="WHIT">        </span><span class="NAME">recty1</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">recty0</span><span class="PUNC">+</span><span class="NAME">w</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>180</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">recty0</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="NAME">mouse.y</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>181</span> </span><span class="WHIT">          </span><span class="NAME">recty0</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">w</span><span class="PUNC">-</span><span class="NAME">h</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>182</span> </span><span class="WHIT">          </span><span class="NAME">recty1</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">w</span><span class="PUNC">-</span><span class="NAME">h</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>183</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>184</span> </span><span class="WHIT">        </span><span class="NAME">h</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">w</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>185</span> </span><span class="WHIT">      </span><span class="PUNC">}</span><span class="WHIT"> </span><span class="KEYW">else</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>186</span> </span><span class="WHIT">        </span><span class="NAME">rectx1</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">rectx0</span><span class="PUNC">+</span><span class="NAME">h</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>187</span> </span><span class="WHIT">        </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">rectx0</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="NAME">mouse.x</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>188</span> </span><span class="WHIT">          </span><span class="NAME">rectx0</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">h</span><span class="PUNC">-</span><span class="NAME">w</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>189</span> </span><span class="WHIT">          </span><span class="NAME">rectx1</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">h</span><span class="PUNC">-</span><span class="NAME">w</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>190</span> </span><span class="WHIT">        </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>191</span> </span><span class="WHIT">        </span><span class="NAME">w</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">h</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>192</span> </span><span class="WHIT">      </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>193</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>194</span> 
<span class='line'>195</span> </span><span class="WHIT">    </span><span class="COMM">// Ellipse radius</span><span class="WHIT">
<span class='line'>196</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">rx</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">w</span><span class="PUNC">/</span><span class="NUMB">2</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>197</span> </span><span class="WHIT">        </span><span class="NAME">ry</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">h</span><span class="PUNC">/</span><span class="NUMB">2</span><span class="PUNC">;</span><span class="WHIT"> 
<span class='line'>198</span> 
<span class='line'>199</span>     </span><span class="COMM">// Ellipse center</span><span class="WHIT">
<span class='line'>200</span> </span><span class="WHIT">    </span><span class="KEYW">var</span><span class="WHIT"> </span><span class="NAME">cx</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">rectx0</span><span class="PUNC">+</span><span class="NAME">rx</span><span class="PUNC">,</span><span class="WHIT">
<span class='line'>201</span> </span><span class="WHIT">        </span><span class="NAME">cy</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">recty0</span><span class="PUNC">+</span><span class="NAME">ry</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>202</span> 
<span class='line'>203</span> </span><span class="WHIT">    </span><span class="COMM">// Ellipse radius*Kappa, for the Bézier curve control points</span><span class="WHIT">
<span class='line'>204</span> </span><span class="WHIT">    </span><span class="NAME">rx</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">K</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>205</span> </span><span class="WHIT">    </span><span class="NAME">ry</span><span class="WHIT"> </span><span class="PUNC">*</span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">K</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>206</span> 
<span class='line'>207</span> </span><span class="WHIT">    </span><span class="NAME">context.beginPath</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>208</span> 
<span class='line'>209</span> </span><span class="WHIT">    </span><span class="COMM">// startX, startY</span><span class="WHIT">
<span class='line'>210</span> </span><span class="WHIT">    </span><span class="NAME">context.moveTo</span><span class="PUNC">(</span><span class="NAME">cx</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">recty0</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>211</span> 
<span class='line'>212</span> </span><span class="WHIT">    </span><span class="COMM">// Control points: cp1x, cp1y, cp2x, cp2y, destx, desty</span><span class="WHIT">
<span class='line'>213</span> </span><span class="WHIT">    </span><span class="COMM">// go clockwise: top-middle, right-middle, bottom-middle, then left-middle</span><span class="WHIT">
<span class='line'>214</span> </span><span class="WHIT">    </span><span class="NAME">context.bezierCurveTo</span><span class="PUNC">(</span><span class="NAME">cx</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">rx</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">recty0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">rectx1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cy</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">ry</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">rectx1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cy</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>215</span> </span><span class="WHIT">    </span><span class="NAME">context.bezierCurveTo</span><span class="PUNC">(</span><span class="NAME">rectx1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cy</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">ry</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cx</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">rx</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">recty1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cx</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">recty1</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>216</span> </span><span class="WHIT">    </span><span class="NAME">context.bezierCurveTo</span><span class="PUNC">(</span><span class="NAME">cx</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">rx</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">recty1</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">rectx0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cy</span><span class="WHIT"> </span><span class="PUNC">+</span><span class="WHIT"> </span><span class="NAME">ry</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">rectx0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cy</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>217</span> </span><span class="WHIT">    </span><span class="NAME">context.bezierCurveTo</span><span class="PUNC">(</span><span class="NAME">rectx0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cy</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">ry</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cx</span><span class="WHIT"> </span><span class="PUNC">-</span><span class="WHIT"> </span><span class="NAME">rx</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">recty0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">cx</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">recty0</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>218</span> 
<span class='line'>219</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">config.shapeType</span><span class="WHIT"> </span><span class="PUNC">!=</span><span class="WHIT"> </span><span class="STRN">'stroke'</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>220</span> </span><span class="WHIT">      </span><span class="NAME">context.fill</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>221</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>222</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">config.shapeType</span><span class="WHIT"> </span><span class="PUNC">!=</span><span class="WHIT"> </span><span class="STRN">'fill'</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>223</span> </span><span class="WHIT">      </span><span class="NAME">context.stroke</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>224</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>225</span> 
<span class='line'>226</span> </span><span class="WHIT">    </span><span class="NAME">context.closePath</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>227</span> 
<span class='line'>228</span> </span><span class="WHIT">    </span><span class="NAME">needsRedraw</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">false</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>229</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>230</span> 
<span class='line'>231</span> </span><span class="WHIT">  </span><span class="COMM">/**
<span class='line'>232</span>    * End the drawing operation, once the user releases the mouse button.
<span class='line'>233</span>    *
<span class='line'>234</span>    * @param {Event} ev The DOM Event object.
<span class='line'>235</span>    */</span><span class="WHIT">
<span class='line'>236</span> </span><span class="WHIT">  </span><span class="NAME">this.mouseup</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">ev</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>237</span> </span><span class="WHIT">    </span><span class="COMM">// Allow click+mousemove, not only mousedown+move+up</span><span class="WHIT">
<span class='line'>238</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">mouse.x</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="NAME">x0</span><span class="WHIT"> </span><span class="PUNC">&&</span><span class="WHIT"> </span><span class="NAME">mouse.y</span><span class="WHIT"> </span><span class="PUNC">==</span><span class="WHIT"> </span><span class="NAME">y0</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>239</span> </span><span class="WHIT">      </span><span class="NAME">mouse.buttonDown</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">true</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>240</span> </span><span class="WHIT">      </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">true</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>241</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>242</span> 
<span class='line'>243</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">timer</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>244</span> </span><span class="WHIT">      </span><span class="NAME">clearInterval</span><span class="PUNC">(</span><span class="NAME">timer</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>245</span> </span><span class="WHIT">      </span><span class="NAME">timer</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">null</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>246</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>247</span> 
<span class='line'>248</span> </span><span class="WHIT">    </span><span class="NAME">shiftKey</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="NAME">ev.shiftKey</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>249</span> </span><span class="WHIT">    </span><span class="NAME">_self.draw</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>250</span> </span><span class="WHIT">    </span><span class="NAME">app.layerUpdate</span><span class="PUNC">(</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>251</span> </span><span class="WHIT">    </span><span class="NAME">gui.statusShow</span><span class="PUNC">(</span><span class="STRN">'ellipseActive'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>252</span> 
<span class='line'>253</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">true</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>254</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>255</span> 
<span class='line'>256</span> </span><span class="WHIT">  </span><span class="COMM">/**
<span class='line'>257</span>    * Allows the user to press &lt;kbd>Escape&lt;/kbd> to cancel the drawing operation.
<span class='line'>258</span>    *
<span class='line'>259</span>    * @param {Event} ev The DOM Event object.
<span class='line'>260</span>    *
<span class='line'>261</span>    * @returns {Boolean} True if the drawing operation was cancelled, or false if 
<span class='line'>262</span>    * not.
<span class='line'>263</span>    */</span><span class="WHIT">
<span class='line'>264</span> </span><span class="WHIT">  </span><span class="NAME">this.keydown</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">function</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">ev</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>265</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="PUNC">!</span><span class="NAME">mouse.buttonDown</span><span class="WHIT"> </span><span class="PUNC">||</span><span class="WHIT"> </span><span class="NAME">ev.kid_</span><span class="WHIT"> </span><span class="PUNC">!=</span><span class="WHIT"> </span><span class="STRN">'Escape'</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>266</span> </span><span class="WHIT">      </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">false</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>267</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>268</span> 
<span class='line'>269</span> </span><span class="WHIT">    </span><span class="KEYW">if</span><span class="WHIT"> </span><span class="PUNC">(</span><span class="NAME">timer</span><span class="PUNC">)</span><span class="WHIT"> </span><span class="PUNC">{</span><span class="WHIT">
<span class='line'>270</span> </span><span class="WHIT">      </span><span class="NAME">clearInterval</span><span class="PUNC">(</span><span class="NAME">timer</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>271</span> </span><span class="WHIT">      </span><span class="NAME">timer</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">null</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>272</span> </span><span class="WHIT">    </span><span class="PUNC">}</span><span class="WHIT">
<span class='line'>273</span> 
<span class='line'>274</span> </span><span class="WHIT">    </span><span class="NAME">context.clearRect</span><span class="PUNC">(</span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NUMB">0</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">image.width</span><span class="PUNC">,</span><span class="WHIT"> </span><span class="NAME">image.height</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>275</span> </span><span class="WHIT">    </span><span class="NAME">mouse.buttonDown</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">false</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>276</span> </span><span class="WHIT">    </span><span class="NAME">needsRedraw</span><span class="WHIT"> </span><span class="PUNC">=</span><span class="WHIT"> </span><span class="KEYW">false</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>277</span> 
<span class='line'>278</span> </span><span class="WHIT">    </span><span class="NAME">gui.statusShow</span><span class="PUNC">(</span><span class="STRN">'ellipseActive'</span><span class="PUNC">)</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>279</span> 
<span class='line'>280</span> </span><span class="WHIT">    </span><span class="KEYW">return</span><span class="WHIT"> </span><span class="KEYW">true</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>281</span> </span><span class="WHIT">  </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>282</span> </span><span class="PUNC">}</span><span class="PUNC">;</span><span class="WHIT">
<span class='line'>283</span> 
<span class='line'>284</span> </span><span class="COMM">// vim:set spell spl=en fo=wan1croqlt tw=80 ts=2 sw=2 sts=2 sta et ai cin fenc=utf-8 ff=unix:</span><span class="WHIT">
<span class='line'>285</span> 
<span class='line'>286</span> </span></pre></body></html>